/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.statusChanges;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.db.Db;

/**
 * A list of GrantRequestStatusChange. Can be loaded from the database.
 * @author DOStudent1
 */
public class GrantRequestStatusChangeList implements Iterable<GrantRequestStatusChange> {

    // the list of all status changes
    private List<GrantRequestStatusChange> statusChangesSet=new LinkedList<GrantRequestStatusChange>();

    // the underlying GrantRequest
    private int grantRequestId;
    
    // initializes the GrantRequestStatusChangeList for the given GrantRequest
    public static GrantRequestStatusChangeList getGrantRequestStatusChangeSetForGrantRequest(int grantRequestId) {
        return(new GrantRequestStatusChangeList(grantRequestId));
    }

    // private constructor
    private GrantRequestStatusChangeList(int grantRequestId) {
        this.grantRequestId = grantRequestId;
        syncDataFromDb();
    }

    // populate the data from the database
    private void syncDataFromDb() {
        try {
            Connection conn=Db.openDbConnection();
            PreparedStatement statusChangesStatement = Db.createPreparedStatement(conn, "SELECT GrantRequestStatusChangeID, StatusChangeType FROM tblGrantRequestsStatusChanges WHERE GrantRequestID=? ORDER BY GrantRequestStatusChangeID");
            statusChangesStatement.setInt(1, grantRequestId);
            statusChangesStatement.executeQuery();
            ResultSet statusChangesResultSet = statusChangesStatement.getResultSet();
            while (statusChangesResultSet.next()) {
                int grantRequestStatusChangeID=statusChangesResultSet.getInt(1);

                // loads the specific status change
                String statusChangeType=statusChangesResultSet.getString(2);
                if (statusChangeType.equals(GrantRequestStatusChangeCancelled.dbString)) {
                    statusChangesSet.add(GrantRequestStatusChangeCancelled.getByStatusChangeId(grantRequestStatusChangeID));
                }
                if (statusChangeType.equals(GrantRequestStatusChangeSubmitted.dbString)) {
                    statusChangesSet.add(GrantRequestStatusChangeSubmitted.getByStatusChangeId(grantRequestStatusChangeID));
                }
                if (statusChangeType.equals(GrantRequestStatusChangeRejected.dbString)) {
                    statusChangesSet.add(GrantRequestStatusChangeRejected.getByStatusChangeId(grantRequestStatusChangeID));
                }
                if (statusChangeType.equals(GrantRequestStatusChangeAwarded.dbString)) {
                    statusChangesSet.add(GrantRequestStatusChangeAwarded.getByStatusChangeId(grantRequestStatusChangeID));
                }
                if (statusChangeType.equals(GrantRequestStatusChangeCompleted.dbString)) {
                    statusChangesSet.add(GrantRequestStatusChangeCompleted.getByStatusChangeId(grantRequestStatusChangeID));
                }
            }
            
            statusChangesResultSet.close();
            statusChangesStatement.close();
            Db.closeDbConnection(conn);
            
        } catch (SQLException ex) {
            Logger.getLogger(GrantRequestStatusChangeList.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    // allows to browse throught the status changes
    public Iterator<GrantRequestStatusChange> iterator() {
        return(statusChangesSet.iterator());
    }
    
    // returns the last status change
    public GrantRequestStatusChange getLastGrantRequestStatusChange() {
        GrantRequestStatusChange res=null;
        for (Iterator<GrantRequestStatusChange> it = statusChangesSet.iterator(); it.hasNext();) {
            res = it.next();
        }
        return(res);
    }
    
}
